﻿using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XinLife.Core.Helper
{
    public class ImageUtil
    {
        /// <summary>
        ///SkiaSharp Base64图片保存
        /// </summary>
        public static void ToImage(string base64String, string outputPath)
        {
            if (base64String.Contains("base64,"))
            {
                base64String = base64String[(base64String.IndexOf(",") + 1)..];
            }
            var imageBytes = Convert.FromBase64String(base64String);
            using var imageStream = new SKMemoryStream(imageBytes);
            using var bitmap = SKBitmap.Decode(imageStream);
            using var image = SKImage.FromBitmap(bitmap);
            using var data = image.Encode(SKEncodedImageFormat.Png, 100);
            using var fileStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write);
            data.SaveTo(fileStream);
        }
        /// <summary>
        ///SkiaSharp 图片转Base64
        /// </summary>
        public static string ImageToBase64(string imagePath)
        {
            string fileExtension = Path.GetExtension(imagePath).ToLower();
            SKEncodedImageFormat imageFormat;
            switch (fileExtension)
            {
                case ".png":
                    imageFormat = SKEncodedImageFormat.Png;
                    break;
                case ".jpg":
                case ".jpeg":
                    imageFormat = SKEncodedImageFormat.Jpeg;
                    break;
                case ".gif":
                    imageFormat = SKEncodedImageFormat.Gif;
                    break;
                case ".ico":
                    imageFormat = SKEncodedImageFormat.Ico;
                    break;
                default:
                    // 默认为未知格式
                    imageFormat = SKEncodedImageFormat.Bmp;
                    break;
            }

            using var image = SKBitmap.Decode(imagePath);
            using var imageStream = new SKDynamicMemoryWStream();
            image.Encode(imageStream, imageFormat, 100);
            var imageBytes = imageStream.DetachAsData().ToArray();
            return Convert.ToBase64String(imageBytes);
            // 创建 SKData 对象并将图片编码为 PNG 格式
            //var data = SKImage.FromBitmap(image).Encode(imageFormat, 100);
            //// 将图片数据转换为 base64 字符串
            //using (var stream = new MemoryStream())
            //{
            //    data.SaveTo(stream);
            //    return Convert.ToBase64String(stream.ToArray());
            //}
        }


        public static string FileToBase64(string imagePath)
        {
            var byteArray = File.ReadAllBytes(imagePath);
            return Convert.ToBase64String(byteArray);
        }
        public void Base64ToFile(string base64String, string outputPath)
        {
            byte[] bt = Convert.FromBase64String(base64String);
            File.WriteAllBytes(outputPath, bt);
        }


    }
}
